<?php
/**
 * 文章分类快讯小工具
 */
class CategoryNewsWidget extends WP_Widget {

    // 初始化小工具
    function __construct() {
        parent::__construct(
            'category_news_widget',
            __('分类快讯', 'text_domain'),
            array( 'description' => __( '显示指定分类的最新文章', 'text_domain' ) )
        );
    }

    // 前端输出
    public function widget( $args, $instance ) {
        $title = apply_filters( 'widget_title', $instance['title'] );
        $category = ! empty( $instance['category'] ) ? $instance['category'] : 0;
        $number = ! empty( $instance['number'] ) ? absint( $instance['number'] ) : 5;
        $show_date = isset( $instance['show_date'] ) ? $instance['show_date'] : false;

        echo $args['before_widget'];
        
        if ( ! empty( $title ) ) {
            echo $args['before_title'] . $title . $args['after_title'];
        }

        $query_args = array(
            'cat'            => $category,
            'posts_per_page' => $number,
            'post_status'    => 'publish',
            'orderby'        => 'date',
            'order'          => 'DESC'
        );

        $news_query = new WP_Query( $query_args );

        if ( $news_query->have_posts() ) {
            echo '<ul class="category-news-list">';
            while ( $news_query->have_posts() ) {
                $news_query->the_post();
                echo '<li>';
                echo '<a href="' . esc_url( get_permalink() ) . '">' . esc_html( get_the_title() ) . '</a>';
                if ( $show_date ) {
                    echo '<span class="post-date">' . esc_html( get_the_date() ) . '</span>';
                }
                echo '</li>';
            }
            echo '</ul>';
            wp_reset_postdata();
        } else {
            echo '<p>' . __( '暂无文章', 'text_domain' ) . '</p>';
        }

        echo $args['after_widget'];
    }

    // 后台表单
    public function form( $instance ) {
        $title = ! empty( $instance['title'] ) ? $instance['title'] : '';
        $category = ! empty( $instance['category'] ) ? $instance['category'] : 0;
        $number = ! empty( $instance['number'] ) ? absint( $instance['number'] ) : 5;
        $show_date = isset( $instance['show_date'] ) ? (bool) $instance['show_date'] : false;
        ?>
        <p>
            <label for="<?php echo $this->get_field_id( 'title' ); ?>">标题：</label>
            <input class="widefat" id="<?php echo $this->get_field_id( 'title' ); ?>" 
                   name="<?php echo $this->get_field_name( 'title' ); ?>" 
                   type="text" value="<?php echo esc_attr( $title ); ?>">
        </p>
        <p>
            <label for="<?php echo $this->get_field_id( 'category' ); ?>">选择分类：</label>
            <?php wp_dropdown_categories(array(
                'show_option_all' => '所有分类',
                'selected'        => $category,
                'name'            => $this->get_field_name( 'category' ),
                'id'              => $this->get_field_id( 'category' ),
                'class'           => 'widefat',
                'hide_empty'      => 1
            )); ?>
        </p>
        <p>
            <label for="<?php echo $this->get_field_id( 'number' ); ?>">显示数量：</label>
            <input class="tiny-text" id="<?php echo $this->get_field_id( 'number' ); ?>"
                   name="<?php echo $this->get_field_name( 'number' ); ?>" 
                   type="number" value="<?php echo $number; ?>" 
                   size="3" min="1" max="20">
        </p>
        <p>
            <input type="checkbox" 
                   id="<?php echo $this->get_field_id( 'show_date' ); ?>" 
                   name="<?php echo $this->get_field_name( 'show_date' ); ?>" 
                   <?php checked( $show_date ); ?>>
            <label for="<?php echo $this->get_field_id( 'show_date' ); ?>">显示发布日期</label>
        </p>
        <?php 
    }

    // 更新设置
    public function update( $new_instance, $old_instance ) {
        $instance = array();
        $instance['title'] = sanitize_text_field( $new_instance['title'] );
        $instance['category'] = absint( $new_instance['category'] );
        $instance['number'] = (int) $new_instance['number'];
        $instance['show_date'] = isset( $new_instance['show_date'] ) ? (bool) $new_instance['show_date'] : false;
        return $instance;
    }
}

// 注册小工具
function register_category_news_widget() {
    register_widget( 'CategoryNewsWidget' );
}
add_action( 'widgets_init', 'register_category_news_widget' );